package cn.appinfodb.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.appinfodb.pojo.AppCategory;
import cn.appinfodb.pojo.DataDictionary;
import cn.appinfodb.pojo.Info;
import cn.appinfodb.service.DevUserService;
import cn.appinfodb.tools.Constants;
import cn.appinfodb.tools.PageSupport;



@Controller
@RequestMapping("/dev/flatform/app")
public class InfoController {
private Logger logger = Logger.getLogger(InfoController.class);
	
	
	@Resource
	private DevUserService devUserService;
	
	
	
	@RequestMapping(value="/list")
	public String getAppInfoList(Model model,HttpSession session,
			                      @RequestParam(value="querySoftwareName",required=false)String querySoftwareName,
			                      @RequestParam(value="queryStatus",required=false)String _queryStatus,
			                      @RequestParam(value="queryFlatformId",required=false) String _queryFlatformId,
			                      @RequestParam(value="queryCategoryLevel1",required=false) String _queryCategoryLevel1,
			                      @RequestParam(value="queryCategoryLevel2",required=false) String _queryCategoryLevel2,
			                      @RequestParam(value="queryCategoryLevel3",required=false) String _queryCategoryLevel3,
			                      @RequestParam(value="pageIndex",required=false) String pageIndex
			                      )
	{
		logger.info("getAppInfoList---->querySoftwareName+"+querySoftwareName);
		logger.info("getAppInfoList---->queryStatus+"+_queryStatus);
		logger.info("getAppInfoList---->queryFlatformId+"+_queryFlatformId);
		logger.info("getAppInfoList---->queryCategoryLevel1+"+_queryCategoryLevel1);
		logger.info("getAppInfoList---->queryCategoryLevel2+"+_queryCategoryLevel2);
		logger.info("getAppInfoList---->queryCategoryLevel3+"+_queryCategoryLevel3);
		logger.info("getAppInfoList---->pageIndex+"+pageIndex);
		List<Info> appInfoList=null;
		List<DataDictionary> statusList=null;
		List<DataDictionary> flatFormList=null;
		List<AppCategory> categoryLevel1List=null;
		List<AppCategory> categoryLevel2List=null;
		List<AppCategory> categoryLevel3List=null;
		int currentPageNo = 1;
		
		int pageSize = Constants.pageSize;
		
		
		Integer devId=null;
		Integer queryStatus=null;
		Integer queryFlatformId=null;
		Integer queryCategoryLevel1=null;
		Integer queryCategoryLevel2=null;
		Integer queryCategoryLevel3=null;
		
		
		
		
		
		
		if(_queryStatus!= null && !_queryStatus.equals("")){
			queryStatus = Integer.parseInt(_queryStatus);
		}
		if(_queryFlatformId!= null && !_queryFlatformId.equals("")){
			queryFlatformId = Integer.parseInt(_queryFlatformId);
		}
		if(_queryCategoryLevel1!= null && !_queryCategoryLevel1.equals("")){
			queryCategoryLevel1 = Integer.parseInt(_queryCategoryLevel1);
		}
		if(_queryCategoryLevel2!= null && !_queryCategoryLevel2.equals("")){
			queryCategoryLevel2 = Integer.parseInt(_queryCategoryLevel2);
		}
		if(_queryCategoryLevel3!= null && !_queryCategoryLevel3.equals("")){
			queryCategoryLevel3 = Integer.parseInt(_queryCategoryLevel3);
		}
		if(pageIndex != null){
    		try{
    			currentPageNo = Integer.valueOf(pageIndex);
    		}catch(NumberFormatException e){
    			e.printStackTrace();
    		}
    	}
		
		//总数量（表）	
    	int totalCount = 0;
		try {
			
			totalCount = devUserService.getInfoCount(querySoftwareName, queryStatus, devId,
					                                     queryFlatformId, queryCategoryLevel1, 
					                                    queryCategoryLevel2, queryCategoryLevel3);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//总页数
    	PageSupport pages=new PageSupport();
    	pages.setCurrentPageNo(currentPageNo);
    	pages.setPageSize(pageSize);
    	pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
    	//控制首页和尾页
    	if(currentPageNo < 1){
    		currentPageNo = 1;
    	}else if(currentPageNo > totalPageCount){
    		currentPageNo = totalPageCount;
    	}
		try {
			appInfoList=devUserService.getInfoList(querySoftwareName, queryStatus, devId,
					                             queryFlatformId, queryCategoryLevel1, 
					                             queryCategoryLevel2, queryCategoryLevel3, 
					                             currentPageNo, pageSize);
			statusList = devUserService.getDataDictionaryList("APP_STATUS");
			flatFormList = devUserService.getDataDictionaryList("APP_FLATFORM");
			categoryLevel1List = devUserService.getAppCategoryListByparentId(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("appInfoList",appInfoList);
		model.addAttribute("statusList",statusList);
		model.addAttribute("flatFormList",flatFormList);
		model.addAttribute("categoryLevel1List",categoryLevel1List);
		model.addAttribute("categoryLevel2List",categoryLevel2List);
		model.addAttribute("categoryLevel3List",categoryLevel3List);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pages",pages);
		model.addAttribute("currentPageNo", currentPageNo);
		model.addAttribute("querySoftwareName",querySoftwareName);
		model.addAttribute("queryStatus",queryStatus);
		model.addAttribute("queryStatus",queryStatus);
		model.addAttribute("queryCategoryLevel1",queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2",queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3",queryCategoryLevel3);
		return "developer/appinfolist";
		
		
	}
	@RequestMapping("/categorylevellist")
	@ResponseBody	
	public List<AppCategory> getCategoryList(@RequestParam(value="pid",required=false)Integer pid)
	{
		List<AppCategory> list=new ArrayList<AppCategory>();
		try {
			list= devUserService.getAppCategoryListByparentId(pid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@RequestMapping(value="/datadictionarylist",method=RequestMethod.GET)
	@ResponseBody
	public List<DataDictionary> getDataDictionaryList (@RequestParam (value="tcode",required=false)String tcode){
		List<DataDictionary> list=new ArrayList<DataDictionary>();
		try {
			list=devUserService.getDataDictionaryList(tcode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping(value="/appinfoadd",method=RequestMethod.GET)
	public String add(@ModelAttribute("Info") Info appInfo){
		return "developer/appinfoadd";
	}  
	
}
